package net.maku.framework.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * HTML转换工具类
 */
public class HtmlUtils {

    /**
     * 将富文本的文字部分取出
     *
     * @param EditorHTML 富文本HTML内容
     * @return Text 文字内容
     */
    public static String transformEditorHTML2Text(String EditorHTML) {
        String text = EditorHTML.replaceAll("</?[^>]+>", "").replaceAll("<a>\\s*|\t|\r|\n</a>", "");
        return text;
    }

    /**
     * 将富文本的图片地址取出
     *
     * @param EditorHTML 富文本HTML内容
     * @return list 图片地址集合
     */
    public static List<String> takeOutIMGFromEditorHTML(String EditorHTML) {
        List<String> list = new ArrayList<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(EditorHTML);
        while (m_image.find()) {
            img = m_image.group();
            if (img.contains("iframe")) {
                String jieguo = img.substring(img.indexOf("iframe") + 1, img.lastIndexOf("/iframe"));//ifram内容
                img = img.replace(jieguo, "");
            }
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }

}
